<?php
include("banner.html");
include("dogs.php");

if(!isset($_SESSION['user'])){
die("Login to use the skin generator");
}

// Om vi har POST verdiene
if(isset($_POST['text-color'], $_POST['back-color'], $_POST['navn'], $_POST['panel-color'])){

$new_skin = "";
$name = $_POST['navn'];


// Safari fix ( safari ignorerer required.. og isset feiler ? sender noe data i tom form?. hm
if(!$name){
die("Du må fylle ut navn.. <a href=skingen.php> >Tilbake< </a>");
}


$line = 0;
$handle = @fopen("Skins/template/styles.css", "r");
if($handle) {
while(($buffer = fgets($handle, 4096)) !== false){
$line++;
//echo "Line: $line: $buffer\n";

// Bakgrunnsfarge ( body bgcolor )..
if(strpos($buffer, "background-color: #151515;") !== false){
$new = "background-color: #".$_POST['back-color'].";";
echo "<span style=\"$buffer\">fant BGcolor @ line $line: $buffer</span> || <span style=\"$new\">Erstattet med $new<br></span>";
$new_skin.=$new;
}


// Bakgrunnsfarge på alle paneler. Banner, sidebar, etc.
else if(strpos($buffer, "background-color: #424242;") !== false){
$new = "background-color: #".$_POST['panel-color'].";";
echo "<span style=\"$buffer\">fant BGcolor @ line $line: $buffer</span> || <span style=\"$new\">Erstattet med $new<br></span>";
$new_skin.=$new;
}

//Font-family
else if(strpos($buffer, "font-family: 'Verdana';") !== false){
$new = "font-family: '".$_POST['font']."';";
$new_skin.=$new;
}


// All font color
else if(strpos($buffer, "color: #FFD840;") !== false){
$new = "color: #".$_POST['text-color'].";";
echo "<span style=\"$buffer\">fant color @ line $line: $buffer</span> || <span style=\"$new\">Erstattet med $new<br></span>";
$new_skin.=$new;
}


//ellers om vi ikke fant noe i linja vi skal erstatte, bare legg til linja i variablen
else {
$new_skin.=$buffer;
}
}

if(!feof($handle)){
	echo "Error fgets failed\n";
}
fclose($handle);
}

//Lagre skin i studenkel_skins ( mysql basen )
if(saveSkin($name, $_SESSION['user']) < 1){
die("et Skin med dette navnet finnes fra før <a href=skingen.php>Retry</a>");
}



//Oprette mappe for skin ( under Skins )
if(!mkdir("Skins/$name")){
die("Folder creation failed: Skins/$name");
}


// Skrive $new_skin til ny fil
file_put_contents("Skins/$name/styles.css", $new_skin);
echo "<br><br>Generert ny CSS: <a href=Skins/$name/styles.css>test.css</a>";

}

// Om vi IKKE har mottat post verdier, vis formen
else {

?>

<!-- HTML START -->
<h2>Studenkel Skin generator</h2><br>


Preview:
<div id="preview" style="border: 1px solid black; width:200px">
<span id="bg" style="font-size:24px;border:1px solid black; width:100%;display:block">Studenkel</span><br><br>
abcdefg<br>
hijklmnopq<br>
<br>
</div>


<!-- NB: Bruk color-codes ( #123456 ) feks herfra: <a href=http://html-color-codes.info/ target="_blank">http://html-color-codes.info</a>  eller her <a href="http://colorschemedesigner.com/" target="_blank">http://colorschemedesigner.com/</a><br> -->
<form method="post" action="skingen.php">
<input type="text" name="navn" placeholder="Navn" required> <br>
<input type="text" name="text-color" id="text-color" placeholder="Font farge" required>
<input type="text" name="back-color" id="back-color" placeholder="BG farge" required>
<input type="text" name="panel-color" id="panel-color" placeholder="BG farge på paneler (banner, sidebar etc)" required><br>
<select name="font">
<option value="Verdana">Verdana</option>
<option value="Arial">Arial</option>
<option value="Cursive">Cursive</option>
<option value="Georgia">Georgia</option>
<option value="Monospace">Monospace</option>
<option value="Fantasy">Fantasy</option>
<option value="Frosty">Frosty</option>
<option value="Helvetica">Helvetica</option>
<option value="Script">Script</option>
</select>
<br>
<input type="button" id="preview_btn" value="Preview">
<hr>
<br><input type="submit" value="Generate">
</form>




<?php
}
?>

<!-- jquery colorpicker -->
<script type="text/javascript" src="colorpicker.js"></script>
<script>
$(function(){
$('#text-color, #back-color, #panel-color').ColorPicker({
	onSubmit: function(hsb, hex, rgb, el) {
		$(el).val(hex);
		$(el).ColorPickerHide();

	},
	onBeforeShow: function () {
		$(this).ColorPickerSetColor(this.value);
	}
})
.bind('keyup', function(){
	$(this).ColorPickerSetColor(this.value);
});


$("#preview_btn").click(function(){		//set preview
		$('#preview').css('background-color', "#"+$("#back-color").val());
		$('#preview').css('color', "#"+$("#text-color").val());		
		$("#bg").css('background', "#"+$("#panel-color").val());
		$("#bg").css('color', "#"+$("#text-color").val());
		$('#preview').css('font-family', $("select[name=font]").val());
		
});

});
</script>



</body>
</html>

